<?php 
class Country_Api_Core extends Core_Api_Abstract{
    

  public static $sitereview = array(
        
        'food' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.pinboard-listings-sitereview'
        ),
        'tourism' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.listings-sitereview'
        ),
        'blog' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.pinboard-listings-sitereview'
        ),
        'fashion' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.pinboard-listings-sitereview'
        ),
        'electronic' => array(
            'sitereview.sponsored-sitereview',
            'sitereview.zerolisting-sitereview',
            'sitereview.recently-popular-random-sitereview',
            'sitereview.slideshow-sitereview',
            'sitereview.category-listings-sitereview'
        ),
        'sport' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.sponsored-sitereview',
            'sitereview.recently-popular-random-sitereview'
        ),
        'classified' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.category-listings-sitereview',
            'sitereview.recently-popular-random-sitereview'
        ),
        'property' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.pinboard-listings-sitereview'
        ),
        'entertainment' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.pinboard-listings-sitereview'
        ),
        'product' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.category-listings-sitereview',
            'sitereview.recently-popular-random-sitereview'
        ),
        'default_type' => array(
            'sitereview.zerolisting-sitereview',
            'sitereview.slideshow-sitereview',
            'sitereview.sponsored-sitereview',
            'sitereview.category-listings-sitereview',
            'sitereview.recently-popular-random-sitereview'
            
        )
  ) ;
  public static $sitereview_browse = array(
        
        'food' => array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview',
            
         ),
        'tourism' => array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview',
            
         ),
        'blog' =>  array(
            'sitereview.browse-listings-sitereview'
            
         ),
        'fashion' =>  array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview',
           
         ),
        'electronic' =>  array(
            'sitereview.sponsored-sitereview',
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview',
            
         ),
        'sport' =>  array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview',
            
         ),
        'classified' => array(
            
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview'
         ),
        'property' =>  array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview'
            
         ),
        'entertainment' =>  array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview',
            'sitereview.slideshow-sitereview'
         ),
         'product' => array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview'
        ),
        'default_type' => array(
            'sitereview.categories-banner-sitereview',
            'sitereview.browse-listings-sitereview',
        )
        
  );
  
  public static $sitereview_profile = array(
        
        'food' => array(
           
            'country.listing-profile'
            
            
         ),
        'tourism' => array(
            
               'country.listing-profile'
            
         ),
        'blog' =>  array(
               'country.listing-profile'
            
         ),
        'fashion' =>  array(
               'country.listing-profile'
           
         ),
        'electronic' =>  array(
               'country.listing-profile'
            
         ),
        'sport' =>  array(
               'country.listing-profile'
            
         ),
        'classified' => array(
              'country.listing-profile'
         ),
        'property' =>  array(
               'country.listing-profile'
            
         ),
        'entertainment' =>  array(
               'country.listing-profile'
            
         ),
         'product' => array(
               'country.listing-profile'
        ),
        'default_type' => array(
               'country.listing-profile'
        )
        
  );
  
  public function getParamWidget($pageName,$name)
  {
     $table = Engine_Api::_()->getDbtable('pages','core');
     $select = $table->select()
                     ->where('name = ?',$pageName);
     $row = $table->fetchRow($select);
     $page_id = $row->page_id;
     
     $table = Engine_Api::_()->getDbtable('content','core');
     $select = $table->select()
                     ->where('page_id = ?',$page_id)
                     ->where('name = ?',$name);
     $row = $table->fetchRow($select);
     $result = $row->params;
     $result['content_id'] = $row->content_id;
     
       
     return $result;
  }
  public function getRating($country_id)
  {
    $table  = Engine_Api::_()->getDbTable('ratings', 'country');
    $rating_sum = $table->select()
      ->from($table->info('name'), new Zend_Db_Expr('SUM(rating)'))
      ->group('country_id')
      ->where('country_id = ?', $country_id)
      ->query()
      ->fetchColumn(0)
      ;

    $total = $this->ratingCount($country_id);
    if ($total) $rating = $rating_sum/$this->ratingCount($country_id);
    else $rating = 0;
    
    return $rating;
  }

  public function getRatings($country_id)
  {
    $table  = Engine_Api::_()->getDbTable('ratings', 'country');
    $rName = $table->info('name');
    $select = $table->select()
                    ->from($rName)
                    ->where($rName.'.country_id = ?', $country_id);
    $row = $table->fetchAll($select);
    return $row;
  }
  
  public function checkRated($country_id, $user_id)
  {
    $table  = Engine_Api::_()->getDbTable('ratings', 'country');

    $rName = $table->info('name');
    $select = $table->select()
                 ->setIntegrityCheck(false)
                    ->where('country_id = ?', $country_id)
                    ->where('user_id = ?', $user_id)
                    ->limit(1);
    $row = $table->fetchAll($select);
    
    if (count($row)>0) return true;
    return false;
  }
  
  public function setRating($country_id, $user_id, $rating){
    $table  = Engine_Api::_()->getDbTable('ratings', 'country');
    $rName = $table->info('name');
    $select = $table->select()
                    ->from($rName)
                    ->where($rName.'.country_id = ?', $country_id)
                    ->where($rName.'.user_id = ?', $user_id);
    $row = $table->fetchRow($select);
    
    if (!$row  ) {
      // create rating
      Engine_Api::_()->getDbTable('ratings', 'country')->insert(array(
        'country_id' => $country_id,
        'user_id' => $user_id,
        'rating' => $rating
      ));
    }

    
  }
  
  public function ratingCount($country_id){
    $table  = Engine_Api::_()->getDbTable('ratings', 'country');
    $rName = $table->info('name');
    $select = $table->select()
                    ->from($rName)
                    ->where($rName.'.country_id = ?', $country_id);
    $row = $table->fetchAll($select);
    $total = count($row);
    return $total;
  }
 public function initTinyMCE()
	{
	  $locale = new Zend_Locale();
	  $lang = $locale->getLanguage();

	  // Initialaze TinyMCE
	  return array(
		'mode'=> "exact",
		'plugins'=> "emotions, xhtmlxtras, preview, paste, style, table, fullscreen, layer",
		'theme'=> "advanced",
		'skin' => 'o2k7',
		'theme_advanced_buttons1'=> "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
		'theme_advanced_buttons2'=> "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		'theme_advanced_buttons3'=> 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,|,fullscreen',
		'height'=> "350",
		'language'=> !empty($lang) ? $lang : 'en',
		'directionality'=> "ltr",
		'upload_url'=> Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'country','controller' => 'news', 'action' => 'upload-photo'), 'default', true),
		'relative_urls'=> "",
		'elements'=> "description,content",
		'width'=> "580",
		'extended_valid_elements'=> "object[width|height|classid|codebase],param[name|value],embed[src|type|width|height|flashvars|wmode]"
		);
	}
	
 public function countFriend($user_id){
    $table = Engine_Api::_()->getDbtable('membership', 'user');
    $select = $table->select()
                    ->from($table->info('name'),"count(*) as total",null)
                    ->where('resource_id = ?',$user_id)
                    ->where('active = 1');
    $rows = $table->fetchRow($select);

    if (!$rows) return 0;
    return $rows->total;
 }
 
 public function getPollDefault(){
    $poll = Engine_Api::_()->getDbtable('polls','country');
    
 }
 
 public function getVotingResultHtml($country_id){
 	$country = Engine_Api::_()->getItem('country', $country_id); 
    $voteOptionTable = Engine_Api::_()->getDbtable('voteoptions','country');
    $select = $voteOptionTable->select();
    $voteOptions  = $voteOptionTable->fetchAll($select);
    
    $zview  = Zend_Registry::get('Zend_View');
	$zview->country = $country;
	$zview->options = $voteOptions;
	$zview->disableVote = true;
	return $zview->render('application/modules/Country/views/scripts/_displayVoteRanker.tpl');
 }
}
?>